<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="司机ID" prop="driverId">
        <el-input
          v-model="queryParams.driverId"
          placeholder="请输入司机ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行名称" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入银行名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行卡号" prop="bankCardId">
        <el-input
          v-model="queryParams.bankCardId"
          placeholder="请输入银行卡号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择提现状态" clearable>
          <el-option
            v-for="dict in withdraw_has_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间" prop="applyDate">
        <el-date-picker clearable
          v-model="queryParams.applyDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择申请时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['qth:driverWithdraw:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['qth:driverWithdraw:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['qth:driverWithdraw:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['qth:driverWithdraw:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="driverWithdrawList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="司机ID" align="center" prop="driverId" />
      <el-table-column label="银行名称" align="center" prop="bankName" />
      <el-table-column label="银行卡号" align="center" prop="bankCardId" />
      <el-table-column label="提现金额" align="center" prop="amount" >
        <template #default="scope">
          {{ formatAmount(scope.row.amount) }}
        </template>
      </el-table-column>
      <el-table-column label="提现状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="withdraw_has_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="applyDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.applyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="finishDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.finishDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-space direction="vertical" :size="8" align="start">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['qth:driverWithdraw:edit']">详情</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['qth:driverWithdraw:remove']">删除</el-button>
            <el-button link type="primary" icon="CircleCheckFilled" @click="handleWithdraw(scope.row)" v-hasPermi="['qth:driverWithdraw:edit']">提现申请</el-button>
          </el-space>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改司机提现申请对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="driverWithdrawRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="司机ID" prop="driverId">
          <el-input v-model="form.driverId" placeholder="请输入司机ID" />
        </el-form-item>
        <el-form-item label="银行名称" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入银行名称" />
        </el-form-item>
        <el-form-item label="银行卡号" prop="bankCardId">
          <el-input v-model="form.bankCardId" placeholder="请输入银行卡号" />
        </el-form-item>
        <el-form-item label="提现金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入提现金额" :disabled="true"/>
        </el-form-item>
        <!-- <el-form-item label="完成时间" prop="finishDate">
          <el-date-picker clearable
            v-model="form.finishDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择完成时间">
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 修改司机提现申请状态对话框 -->
    <el-dialog :title="`提现申请`" v-model="withdrawOpen" width="400px" append-to-body>
      <el-form ref="withdrawRef" :model="withdrawForm" :relues="withdrawRules" label-width="80px">
        <el-form-item label="提现状态" prop="status">
          <el-select v-model="withdrawForm.status" placeholder="请选择提现状态">
            <el-option
              v-for="dict in withdraw_has_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitWithdrawForm">确 定</el-button>
        <el-button @click="cancelWithdraw">取 消</el-button>
      </div>
    </template>
    </el-dialog>
  </div>
</template>

<script setup name="DriverWithdraw">
import { updateWithdrawStatus, listDriverWithdraw, getDriverWithdraw, delDriverWithdraw, addDriverWithdraw, updateDriverWithdraw } from "@/api/qth/driverWithdraw";
import { ref } from "vue";
import { formatAmount } from "@/utils/amountUtil.js"

const { proxy } = getCurrentInstance();
const { withdraw_has_status } = proxy.useDict('withdraw_has_status');

const driverWithdrawList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const withdrawOpen = ref(false);
const withdrawForm = ref({ id: null, status: null })
const withdrawRules = ref({status: [{required: true, trigger: "change"}]})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    driverId: null,
    bankName: null,
    bankCardId: null,
    status: null,
    applyDate: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询司机提现申请列表 */
function getList() {
  loading.value = true;
  listDriverWithdraw(queryParams.value).then(response => {
    driverWithdrawList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    driverId: null,
    bankName: null,
    bankCardId: null,
    amount: null,
    status: null,
    applyDate: null,
    finishDate: null
  };
  proxy.resetForm("driverWithdrawRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加司机提现申请";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getDriverWithdraw(_id).then(response => {
    form.value = response.data;
    const couponData = response.data;
    form.value = {
      ...couponData,
      amount: formatAmount(couponData.amount || 0) 
    }
    open.value = true;
    title.value = "修改司机提现申请";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["driverWithdrawRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateDriverWithdraw(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDriverWithdraw(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除司机提现申请编号为"' + _ids + '"的数据项？').then(function() {
    return delDriverWithdraw(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('qth/driverWithdraw/export', {
    ...queryParams.value
  }, `driverWithdraw_${new Date().getTime()}.xlsx`)
}

/** 打开提现申请状态对话框 */
function handleWithdraw(row) {
  if ([1, 2].includes(row.status)){
    proxy.$message.warning("该订单已执行过操作，无法重复申请！");
    return;
  }
  withdrawForm.value.id = row.id;
  withdrawForm.value.status = row.status;
  withdrawOpen.value = true;
}

/** 打开提现申请状态对话框 */
function cancelWithdraw() {
  withdrawOpen.value = false;
  resetWithdrawForm();
}

/** 重置提现申请状态表单*/  
function resetWithdrawForm() {
  withdrawForm.value = {
    id: null,
    status: null
  };
  proxy.resetForm("withdrawRef");
}

/** 提交提现申请状态修改*/  
function submitWithdrawForm(){
  proxy.$refs["withdrawRef"].validate(async (valid) => {
    if(valid){
      console.log("提交给后端的id， status：", {
        id: withdrawForm.value.id,
        status: withdrawForm.value.status
      });
        updateWithdrawStatus({
        id: withdrawForm.value.id,
        status:withdrawForm.value.status
      }).then(response => {
        proxy.$modal.msgSuccess("操作成功");
        withdrawOpen.value = false;
        getList();
        resetWithdrawForm();
      })
    }
  })
}


getList();
</script>
